﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;

namespace Migracion.Process
{
    public class AddRemoveFile
    {
        public void ExtaerArchivo(string filePath, string selectStatement)
        {
            if (!String.IsNullOrEmpty(selectStatement) && !String.IsNullOrEmpty(filePath))
            {
                string constr = ConfigurationManager.ConnectionStrings["PE_ONLINE_PRODUCCIONSelect"].ConnectionString;

                using (StreamWriter streamWriter = new StreamWriter(filePath))
                {
                    using (DbConnection connection = new SqlConnection(constr))
                    {
                        try
                        {
                            connection.Open();
                            DbCommand command = connection.CreateCommand();
                            command.CommandText = selectStatement;
                            command.CommandType = System.Data.CommandType.Text;

                            using (DbDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    string text = reader.GetString(0);
                                    streamWriter.Write(text);
                                }

                                reader.Close();
                                streamWriter.Close();
                            }
                        }
                        finally
                        {
                            connection.Close();
                            streamWriter.Close();
                        }
                    }
                }
            }
        }

        public void IncorporarArchivo(string filePath, string updateStatement)
        {
            if (!String.IsNullOrEmpty(updateStatement) && !String.IsNullOrEmpty(filePath))
            {
                string constr = ConfigurationManager.ConnectionStrings["PE_ONLINE_PRODUCCIONSelect"].ConnectionString;

                using (StreamReader streamReader = new StreamReader(filePath))
                {
                    using (DbConnection connection = new SqlConnection(constr))
                    {
                        try
                        {
                            connection.Open();
                            DbCommand command = connection.CreateCommand();
                            command.CommandText = updateStatement;
                            command.CommandType = System.Data.CommandType.Text;
                            DbParameter param = command.CreateParameter();

                            string text = streamReader.ReadToEnd();
                            streamReader.Close();
                            param.ParameterName = "@archivo";
                            param.Value = text;
                            command.Parameters.Add(param);

                            command.ExecuteNonQuery();                                                                                     
                        }
                        finally
                        {
                            connection.Close();
                            streamReader.Close();
                        }
                    }
                }
            }
        }
    }
}
